package com.huizhonglm.diap.core.shake;

import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.core.utils.ToastUtil;
import com.huizhonglm.diap.ui.BaseActivity;

import java.io.IOException;

/**
 * 摇一摇
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-04-26 10:17
 */
public class ShakeActivityCapture extends BaseActivity implements OnShakeListener {
    private static final long VIBRATE_DURATION = 200L;

    private ShakeManager shakeMgr;


    private MediaPlayer mediaPlayer;
    private boolean playShake;
    private static final float SHAKE_VOLUME = 0.50f;
    private boolean vibrate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shake_capture);
        setBackEvent(this, null);
        shakeMgr = new ShakeManager(this);
        shakeMgr.setOnShakeListener(this);
    }

    @Override
    protected void assignViews() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        playShake = true;
        AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
        if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) {
            playShake = false;
        }
        initShakeSound();
        vibrate = true;
    }

    @Override
    public void onShake() {
        shakeMgr.stop();
        playShakeSoundAndVibrate();
        ToastUtil.makeTextShort("摇到了");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                shakeMgr.start();
            }
        }, 2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (shakeMgr != null) {
            shakeMgr.stop();
        }
    }

    private void initShakeSound() {
        if (playShake && mediaPlayer == null) {
            setVolumeControlStream(AudioManager.STREAM_MUSIC);
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer mediaPlayer) {
                    mediaPlayer.seekTo(0);
                }
            });

            AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.shake);
            try {
                mediaPlayer.setDataSource(file.getFileDescriptor(),
                        file.getStartOffset(), file.getLength());
                file.close();
                mediaPlayer.setVolume(SHAKE_VOLUME, SHAKE_VOLUME);
                mediaPlayer.prepare();
            } catch (IOException e) {
                mediaPlayer = null;
            }
        }
    }

    private void playShakeSoundAndVibrate() {
        if (vibrate) {
            Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
            vibrator.vibrate(VIBRATE_DURATION);
        }

        if (playShake && mediaPlayer != null) {
            mediaPlayer.start();
        }
    }
}
